<!DOCTYPE html><html lang="en"><head><meta charset="UTF-8"><meta http-equiv="X-UA-Compatible" content="IE=edge"><meta name="viewport" content="width=device-width, initial-scale=1.0"><meta name="theme-color" content="#0078E7"><meta name="author" content="zzl"><meta name="copyright" content="zzl"><meta name="generator" content="Hexo 5.4.0"><meta name="theme" content="hexo-theme-yun"><title>Spring学习 | zzl</title><link rel="stylesheet" href="https://fonts.googleapis.com/css2?family=Noto+Serif+SC:wght@900&amp;display=swap" media="print" onload="this.media='all'"><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/star-markdown-css@0.1.25/dist/yun/yun-markdown.min.css"><script src="//at.alicdn.com/t/font_1140697_dxory92pb0h.js" async></script><script src="https://cdn.jsdelivr.net/npm/scrollreveal/dist/scrollreveal.min.js" defer></script><script>document.addEventListener("DOMContentLoaded", () => {
  [".post-card",".post-content img"].forEach((target)=> {
    ScrollReveal().reveal(target);
  })
});
</script><link rel="icon" href="/web/yun.svg"><link rel="mask-icon" href="/web/yun.svg" color="#0078E7"><link rel="alternate icon" href="/web/yun.ico"><link rel="preload" href="/web/css/hexo-theme-yun.css" as="style"><link rel="preload" href="/web/js/utils.js" as="script"><link rel="preload" href="/web/js/hexo-theme-yun.js" as="script"><link rel="prefetch" href="/web/js/sidebar.js" as="script"><link rel="preconnect" href="https://cdn.jsdelivr.net" crossorigin><script id="yun-config">
    const Yun = window.Yun || {};
    window.CONFIG = {"hostname":"zilongzeng.gitee.io","root":"/web/","title":"奏者的小站","version":"1.6.2","mode":"auto","copycode":true,"page":{"isPost":true},"i18n":{"placeholder":"Searching...","empty":"We didn't find any results for the search: ${query}.","hits":"${hits} results found","hits_time":"${hits} results found in ${time} ms"},"anonymous_image":"https://cdn.jsdelivr.net/gh/YunYouJun/cdn/img/avatar/none.jpg","say":{"api":"https://v1.hitokoto.cn","hitokoto":true},"fireworks":{"colors":["102, 167, 221","62, 131, 225","33, 78, 194"]}};
  </script><link rel="stylesheet" href="/web/css/hexo-theme-yun.css"><script src="/web/js/utils.js"></script><script src="/web/js/hexo-theme-yun.js"></script><meta name="description" content="SpringSpring框架的使用Spring:（1）作为对象的容器，对象不再直接通过new的方式产生，而是由Spring容器来提供（2）控制反转：解除复杂对象之间的直接依赖关系，由Spring容器来控制对象之间的依赖关系 12345678910111213141516--- 利用 Spring 讲解  大侠(Hero)   与      刀(Sword) 的故事(1) 创建项目 springde">
<meta property="og:type" content="article">
<meta property="og:title" content="Spring学习">
<meta property="og:url" content="http://zilongzeng.gitee.io/web/2022/04/28/Spring%E5%AD%A6%E4%B9%A0/index.html">
<meta property="og:site_name" content="zzl">
<meta property="og:description" content="SpringSpring框架的使用Spring:（1）作为对象的容器，对象不再直接通过new的方式产生，而是由Spring容器来提供（2）控制反转：解除复杂对象之间的直接依赖关系，由Spring容器来控制对象之间的依赖关系 12345678910111213141516--- 利用 Spring 讲解  大侠(Hero)   与      刀(Sword) 的故事(1) 创建项目 springde">
<meta property="og:locale" content="en_US">
<meta property="article:published_time" content="2022-04-28T06:56:16.000Z">
<meta property="article:modified_time" content="2022-04-28T06:57:24.863Z">
<meta property="article:author" content="zzl">
<meta property="article:tag" content="java">
<meta name="twitter:card" content="summary"><script src="/web/js/ui/mode.js"></script></head><body><script defer src="https://cdn.jsdelivr.net/npm/animejs@latest"></script><script defer src="/web/js/ui/fireworks.js"></script><canvas class="fireworks"></canvas><div class="container"><a class="sidebar-toggle hty-icon-button" id="menu-btn"><div class="hamburger hamburger--spin" type="button"><span class="hamburger-box"><span class="hamburger-inner"></span></span></div></a><div class="sidebar-toggle sidebar-overlay"></div><aside class="sidebar"><script src="/web/js/sidebar.js"></script><ul class="sidebar-nav"><li class="sidebar-nav-item sidebar-nav-toc hty-icon-button sidebar-nav-active" data-target="post-toc-wrap" title="Table of Contents"><svg class="icon" aria-hidden="true"><use xlink:href="#icon-list-ordered"></use></svg></li><li class="sidebar-nav-item sidebar-nav-overview hty-icon-button" data-target="site-overview-wrap" title="Overview"><svg class="icon" aria-hidden="true"><use xlink:href="#icon-passport-line"></use></svg></li></ul><div class="sidebar-panel" id="site-overview-wrap"><div class="site-info fix-top"><a class="site-author-avatar" href="/web/about/" title="zzl"><img width="96" loading="lazy" src="/web/zzl.jpg" alt="zzl"><span class="site-author-status" title="永远相信美好的事情即将发生">😊</span></a><div class="site-author-name"><a href="/web/about/">zzl</a></div><span class="site-name">zzl</span><sub class="site-subtitle"></sub><div class="site-desciption"></div></div><nav class="site-state"><a class="site-state-item hty-icon-button icon-home" href="/web/" title="Home"><span class="site-state-item-icon"><svg class="icon" aria-hidden="true"><use xlink:href="#icon-home-4-line"></use></svg></span></a><div class="site-state-item"><a href="/web/archives/" title="Archives"><span class="site-state-item-icon"><svg class="icon" aria-hidden="true"><use xlink:href="#icon-archive-line"></use></svg></span><span class="site-state-item-count">16</span></a></div><div class="site-state-item"><a href="/web/categories/" title="Categories"><span class="site-state-item-icon"><svg class="icon" aria-hidden="true"><use xlink:href="#icon-folder-2-line"></use></svg></span><span class="site-state-item-count">0</span></a></div><div class="site-state-item"><a href="/web/tags/" title="Tags"><span class="site-state-item-icon"><svg class="icon" aria-hidden="true"><use xlink:href="#icon-price-tag-3-line"></use></svg></span><span class="site-state-item-count">9</span></a></div><a class="site-state-item hty-icon-button" target="_blank" rel="noopener" href="https://yun.yunyoujun.cn" title="文档"><span class="site-state-item-icon"><svg class="icon" aria-hidden="true"><use xlink:href="#icon-settings-line"></use></svg></span></a></nav><hr style="margin-bottom:0.5rem"><div class="links-of-author"><a class="links-of-author-item hty-icon-button" rel="noopener" href="/atom.xml" title="RSS" target="_blank" style="color:orange"><svg class="icon" aria-hidden="true"><use xlink:href="#icon-rss-line"></use></svg></a><a class="links-of-author-item hty-icon-button" rel="noopener" title="QQ 2915667803" target="_blank" style="color:#12B7F5"><svg class="icon" aria-hidden="true"><use xlink:href="#icon-qq-line"></use></svg></a><a class="links-of-author-item hty-icon-button" rel="noopener" title="GitHub" target="_blank" style="color:#6e5494"><svg class="icon" aria-hidden="true"><use xlink:href="#icon-github-line"></use></svg></a><a class="links-of-author-item hty-icon-button" rel="noopener" title="微博" target="_blank" style="color:#E6162D"><svg class="icon" aria-hidden="true"><use xlink:href="#icon-weibo-line"></use></svg></a><a class="links-of-author-item hty-icon-button" rel="noopener" title="豆瓣" target="_blank" style="color:#007722"><svg class="icon" aria-hidden="true"><use xlink:href="#icon-douban-line"></use></svg></a><a class="links-of-author-item hty-icon-button" rel="noopener" title="网易云音乐" target="_blank" style="color:#C20C0C"><svg class="icon" aria-hidden="true"><use xlink:href="#icon-netease-cloud-music-line"></use></svg></a><a class="links-of-author-item hty-icon-button" rel="noopener" title="知乎" target="_blank" style="color:#0084FF"><svg class="icon" aria-hidden="true"><use xlink:href="#icon-zhihu-line"></use></svg></a><a class="links-of-author-item hty-icon-button" rel="noopener" title="哔哩哔哩" target="_blank" style="color:#FF8EB3"><svg class="icon" aria-hidden="true"><use xlink:href="#icon-bilibili-line"></use></svg></a><a class="links-of-author-item hty-icon-button" rel="noopener" title="微信公众号" target="_blank" style="color:#1AAD19"><svg class="icon" aria-hidden="true"><use xlink:href="#icon-wechat-2-line"></use></svg></a><a class="links-of-author-item hty-icon-button" rel="noopener" title="Twitter" target="_blank" style="color:#1da1f2"><svg class="icon" aria-hidden="true"><use xlink:href="#icon-twitter-line"></use></svg></a><a class="links-of-author-item hty-icon-button" rel="noopener" title="Telegram Channel" target="_blank" style="color:#0088CC"><svg class="icon" aria-hidden="true"><use xlink:href="#icon-telegram-line"></use></svg></a><a class="links-of-author-item hty-icon-button" rel="noopener" title="E-Mail" target="_blank" style="color:#8E71C1"><svg class="icon" aria-hidden="true"><use xlink:href="#icon-mail-line"></use></svg></a><a class="links-of-author-item hty-icon-button" rel="noopener" title="Travelling" target="_blank" style="color:var(--hty-text-color)"><svg class="icon" aria-hidden="true"><use xlink:href="#icon-train-line"></use></svg></a></div><hr style="margin:0.5rem 1rem"><div class="links"><a class="links-item hty-icon-button" href="/web/links/" title="我的小伙伴们" style="color:dodgerblue"><svg class="icon" aria-hidden="true"><use xlink:href="#icon-genderless-line"></use></svg></a></div><br><a class="links-item hty-icon-button" id="toggle-mode-btn" href="javascript:;" title="Mode" style="color: #f1cb64"><svg class="icon" aria-hidden="true"><use xlink:href="#icon-contrast-2-line"></use></svg></a></div><div class="sidebar-panel sidebar-panel-active" id="post-toc-wrap"><div class="post-toc"><div class="post-toc-content"><ol class="toc"><li class="toc-item toc-level-1"><a class="toc-link" href="#Spring"><span class="toc-number">1.</span> <span class="toc-text">Spring</span></a><ol class="toc-child"><li class="toc-item toc-level-2"><a class="toc-link" href="#Spring%E6%A1%86%E6%9E%B6%E7%9A%84%E4%BD%BF%E7%94%A8"><span class="toc-number">1.1.</span> <span class="toc-text">Spring框架的使用</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#Spring%E4%BD%BF%E7%94%A8%EF%BC%88%E7%BB%AD%EF%BC%89"><span class="toc-number">1.2.</span> <span class="toc-text">Spring使用（续）</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#SpringMVC"><span class="toc-number">1.3.</span> <span class="toc-text">SpringMVC</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#%E5%AE%9E%E7%8E%B0"><span class="toc-number">1.3.1.</span> <span class="toc-text">实现</span></a></li></ol></li></ol></li></ol></div></div></div></aside><main class="sidebar-translate" id="content"><div id="post"><article class="hty-card post-block" itemscope itemtype="https://schema.org/Article"><link itemprop="mainEntityOfPage" href="http://zilongzeng.gitee.io/web/web/2022/04/28/Spring%E5%AD%A6%E4%B9%A0/"><span hidden itemprop="author" itemscope itemtype="https://schema.org/Person"><meta itemprop="name" content="zzl"><meta itemprop="description"></span><span hidden itemprop="publisher" itemscope itemtype="https://schema.org/Organization"><meta itemprop="name" content="zzl"></span><header class="post-header"><h1 class="post-title" itemprop="name headline">Spring学习</h1><div class="post-meta"><div class="post-time" style="display:inline-block"><span class="post-meta-item-icon"><svg class="icon" aria-hidden="true"><use xlink:href="#icon-calendar-line"></use></svg></span> <time title="Created: 2022-04-28 14:56:16" itemprop="dateCreated datePublished" datetime="2022-04-28T14:56:16+08:00">2022-04-28</time></div><div class="post-classify"><span class="post-tag"><a class="tag-item" href="/web/tags/java/" style="--text-color:var(--hty-text-color)"><span class="post-meta-item-icon"><svg class="icon" aria-hidden="true"><use xlink:href="#icon-price-tag-3-line"></use></svg></span><span class="tag-name">java</span></a></span></div></div></header><section class="post-body" itemprop="articleBody"><div class="post-content markdown-body" style="--smc-primary:#0078E7;"><h1 id="Spring"><a href="#Spring" class="headerlink" title="Spring"></a>Spring</h1><h2 id="Spring框架的使用"><a href="#Spring框架的使用" class="headerlink" title="Spring框架的使用"></a>Spring框架的使用</h2><p>Spring:<br>（1）作为对象的容器，对象不再直接通过new的方式产生，而是由Spring容器来提供<br>（2）控制反转：解除复杂对象之间的直接依赖关系，由Spring容器来控制对象之间的依赖关系</p>
<figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">--- 利用 Spring 讲解  大侠(Hero)   与      刀(Sword) 的故事</span></span><br><span class="line">(<span class="number">1</span>) 创建项目 springdemo   创建包 cn.jju.it</span><br><span class="line">(<span class="number">2</span>) 创建类Sword</span><br><span class="line">(<span class="number">3</span>) 创建类Hero</span><br><span class="line">(<span class="number">4</span>) 新建lib目录，将spring相关的jar包拷贝进来，配置bulid<span class="operator">-</span>path</span><br><span class="line">(<span class="number">5</span>)在src下面，新建spring主配置文件application.xml</span><br><span class="line">(<span class="number">6</span>)配置bean的创建</span><br><span class="line"></span><br><span class="line"><span class="comment">--知识点：面向接口的编程（多态）</span></span><br><span class="line">(<span class="number">1</span>)定义有关刀的接口（interface）ISword，增加一个attack（）方法的声明</span><br><span class="line"></span><br><span class="line">(<span class="number">2</span>)在Sword类中，实现ISword接口</span><br><span class="line">(<span class="number">3</span>)新增GoldenSword类，实现ISword接口</span><br><span class="line">(<span class="number">4</span>)将Hero类中的sword属性类型改成ISword</span><br><span class="line"></span><br><span class="line"></span><br></pre></td></tr></table></figure>


<h2 id="Spring使用（续）"><a href="#Spring使用（续）" class="headerlink" title="Spring使用（续）"></a>Spring使用（续）</h2><p>使用xml来配置对象的创建 和对象的依赖注入，当项目里面的对象很多,维护该xml很麻烦，spring提供一种更直观的 在代码里面，通过“注解”来实现对象的创建和依赖注入</p>
<figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">---例：</span></span><br><span class="line"><span class="comment">---公主 与 琴 的故事</span></span><br><span class="line"><span class="string">&#x27;注解&#x27;</span>：</span><br><span class="line">（<span class="number">1</span>）在类的定义上面，增加<span class="variable">@Component</span>，后面可以通过类名首字母小写作为id从spring容器中获得对象</span><br><span class="line">（<span class="number">2</span>）通过<span class="variable">@AutoWired</span>来设置注入依赖另外一个对象，且不需要写<span class="keyword">get</span>和<span class="keyword">set</span>方法</span><br><span class="line">（<span class="number">3</span>）在spring的配置文件application.xml中，配置需要进行注解解析的类所在包</span><br><span class="line">（<span class="number">4</span>）</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="comment">---Spring-Mybatis的整合</span></span><br><span class="line">核心要点：spring接管mybatis里面相关对象的创建</span><br><span class="line">要解决的问题：在controller类里面，实现通过<span class="variable">@Autowire</span>注入一个Mapper</span><br><span class="line"><span class="comment">--例</span></span><br><span class="line"><span class="variable">@component</span></span><br><span class="line">class TeacherController&#123;</span><br><span class="line">    <span class="variable">@Autowire</span></span><br><span class="line">    TacherMapper mapper;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="comment">---整合过程</span></span><br><span class="line">（<span class="number">1</span>）创建项目spring_mybatis</span><br><span class="line">（<span class="number">2</span>）创建lib目录，将相关jar包拷贝：<span class="number">1</span>、spring <span class="number">2</span>、mybatis <span class="number">3</span>、spring<span class="operator">-</span>mybatis</span><br><span class="line">（<span class="number">3</span>）配置build<span class="operator">-</span>path,并在src下创建<span class="number">3</span>个配置文件：<span class="number">1</span>、db.property  <span class="number">2</span>、mybatis<span class="operator">-</span>config.xml <span class="number">3</span>、application.xml</span><br><span class="line">（<span class="number">4</span>）创建包：controller mapper model</span><br><span class="line"><span class="comment">---配置application.xml</span></span><br><span class="line"><span class="number">1</span>、配置生成sqlSessionFactoryBean对象</span><br><span class="line"><span class="number">2</span>、配置mapperScanner：将指定包里面所有的mapper接口按照mybatis的方式实例化对象</span><br></pre></td></tr></table></figure>


<h2 id="SpringMVC"><a href="#SpringMVC" class="headerlink" title="SpringMVC"></a>SpringMVC</h2><p>系统需要向外提供http服务，提供url来进行访问，url被映射到某个控制器的某个方法</p>
<figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">---SpringMVC的基本原理</span></span><br><span class="line">（<span class="number">1</span>）用户在网页上进行操作，通过浏览器发送请求（url）到web服务器</span><br><span class="line">（<span class="number">2</span>）web服务器根据url将请求发送给SpringMVC的DispatcherServlet处理</span><br><span class="line">（<span class="number">3</span>）DispatcherServlet通过HandleMapping查找该url所映射的控制器<span class="comment">---控制器类定义中声明</span></span><br><span class="line">（<span class="number">4</span>）执行控制器的相应方法</span><br><span class="line">（<span class="number">5</span>）返回结果给浏览器：<span class="number">1</span>、ModelAndView对象（指明跳转的url和传递的数据）<span class="number">2</span>、纯JSON数据（适用前后端分离的项目）</span><br><span class="line"></span><br><span class="line"></span><br></pre></td></tr></table></figure>

<h3 id="实现"><a href="#实现" class="headerlink" title="实现"></a>实现</h3><figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">---1、创建web项目</span></span><br><span class="line"></span><br><span class="line"></span><br></pre></td></tr></table></figure></div><div id="reward-container"><span class="hty-icon-button button-glow" id="reward-button" title="Donate" onclick="var qr = document.getElementById(&quot;qr&quot;); qr.style.display = (qr.style.display === &quot;none&quot;) ? &quot;block&quot; : &quot;none&quot;;"><svg class="icon" aria-hidden="true"><use xlink:href="#icon-hand-coin-line"></use></svg></span><div id="reward-comment">I want you.</div><div id="qr" style="display:none;"><div style="display:inline-block"></div><div style="display:inline-block"></div><div style="display:inline-block"></div></div></div><ul class="post-copyright"><li class="post-copyright-author"><strong>Post author: </strong>zzl</li><li class="post-copyright-link"><strong>Post link: </strong><a href="http://zilongzeng.gitee.io/web/2022/04/28/Spring%E5%AD%A6%E4%B9%A0/" title="Spring学习">http://zilongzeng.gitee.io/web/2022/04/28/Spring%E5%AD%A6%E4%B9%A0/</a></li><li class="post-copyright-license"><strong>Copyright Notice: </strong>All articles in this blog are licensed under <a href="https://creativecommons.org/licenses/by-nc-sa/4.0/deed.zh" target="_blank" rel="noopener" title="CC BY-NC-SA 4.0 "><svg class="icon"><use xlink:href="#icon-creative-commons-line"></use></svg><svg class="icon"><use xlink:href="#icon-creative-commons-by-line"></use></svg><svg class="icon"><use xlink:href="#icon-creative-commons-nc-line"></use></svg><svg class="icon"><use xlink:href="#icon-creative-commons-sa-line"></use></svg></a> unless otherwise stated.</li></ul></section></article><div class="post-nav"><div class="post-nav-item"><a class="post-nav-prev" href="/web/2022/05/05/java%E5%90%8E%E7%AB%AFlist%E8%BF%94%E5%9B%9Ejson%E7%BB%93%E6%9E%9C%E6%97%B6%E5%87%BA%E7%8E%B0-data-0-objCollege/" rel="prev" title="java后端list返回json结果时出现:&quot;$ref&quot;: &quot;$.data[0].objCollege&quot;"><svg class="icon" aria-hidden="true"><use xlink:href="#icon-arrow-left-s-line"></use></svg><span class="post-nav-text">java后端list返回json结果时出现:&quot;$ref&quot;: &quot;$.data[0].objCollege&quot;</span></a></div><div class="post-nav-item"><a class="post-nav-next" href="/web/2022/04/28/sql%E8%AF%AD%E8%A8%80/" rel="next" title="sql语言"><span class="post-nav-text">sql语言</span><svg class="icon" aria-hidden="true"><use xlink:href="#icon-arrow-right-s-line"></use></svg></a></div></div></div><div class="hty-card" id="comment"><div class="comment-tooltip text-center"><span>要不要和我说些什么？</span><br></div></div></main><footer class="sidebar-translate" id="footer"><div class="copyright"><span>&copy; 2019 – 2022 </span><span class="with-love" id="animate"><svg class="icon" aria-hidden="true"><use xlink:href="#icon-cloud-line"></use></svg></span><span class="author"> zzl</span></div><div class="powered"><span>Powered by <a href="https://hexo.io" target="_blank" rel="noopener">Hexo</a> v5.4.0</span><span class="footer-separator">|</span><span>Theme - <a rel="noopener" href="https://github.com/YunYouJun/hexo-theme-yun" target="_blank"><span>Yun</span></a> v1.6.2</span></div></footer><a class="hty-icon-button" id="back-to-top" aria-label="back-to-top" href="#"><svg class="icon" aria-hidden="true"><use xlink:href="#icon-arrow-up-s-line"></use></svg><svg class="progress-circle-container" viewBox="0 0 100 100"><circle class="progress-circle" id="progressCircle" cx="50" cy="50" r="48" fill="none" stroke="#0078E7" stroke-width="2" stroke-linecap="round"></circle></svg></a></div></body></html>